package com.example.qxfw.dataBaseManager.nyzjk.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.CropGrowdateIndexlib;
import com.example.qxfw.common.entity.ny.StationInfo;
import com.example.qxfw.common.entity.qx.SmallScaleStation;
import com.example.qxfw.dataBaseManager.nyzjk.service.FyqclzlkService;
import com.example.qxfw.dataBaseManager.nyzjk.service.NyfwzdkService;
import com.example.qxfw.dataBaseManager.service.QxxxzService;
import com.example.qxfw.mongodb.hy.entity.HyImageFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/6  星期四   2:04
 * @description : 此类用来  农业服务站站点库
 ***************************************/
@Slf4j
@Api(tags = "数据库管理-农业专家库-农业服务站站点库")
@RestController
@CrossOrigin
@RequestMapping("/NyfwzdkController")
public class NyfwzdkController {

    @Autowired
    private FyqclzlkService fyqclzlkService;

    @Autowired
    private NyfwzdkService nyfwzdkService;

    @Autowired
    private QxxxzService qxxxzService;

    @Value("${province}")
    private String m_province;

    @Value("${city}")
    private String m_city;

    @ApiOperation("初始化站点信息")
    @GetMapping("getAllOrder")
    public SysResult getAllOrder() {
        List<StationInfo> allOrder = this.nyfwzdkService.getAllOrder();
        return SysResult.success(allOrder);
    }


    @ApiOperation("得到市")
    @GetMapping("querySelect")
    public SysResult querySelect() {
        List<SmallScaleStation> smallScaleStations = this.qxxxzService.querySelect(m_province, m_city);
        return SysResult.success(smallScaleStations);
    }


    @ApiOperation("得到县")
    @GetMapping("selectDistinct")
    public SysResult selectDistinct(String city) {
        List<SmallScaleStation> smallScaleStationList = this.qxxxzService.selectDistinct(city);
        return SysResult.success(smallScaleStationList);
    }

    @ApiOperation("查询作物名称")
    @GetMapping("selectDistinctCropName")
    public SysResult selectDistinctCropName() {
        List<CropGrowdateIndexlib> cropGrowdateIndexlibList = this.fyqclzlkService.querySelect("cropname", null);
        return SysResult.success(cropGrowdateIndexlibList);
    }

    @ApiOperation("添加")
    @PostMapping("saveStationId")
    public SysResult saveStationId(@RequestBody StationInfo stationInfo) {
        List<StationInfo> stationInfoList = this.nyfwzdkService.selectByStationId(stationInfo.getStationid());
        if (stationInfoList.size() > 0) {
            return SysResult.fail("存在相同站点编号");
        } else {
            Integer saveStationInfo = this.nyfwzdkService.saveStationInfo(stationInfo);
            if (saveStationInfo > 0) {
                return SysResult.success();
            } else {
                return SysResult.fail();
            }
        }
    }

    @ApiOperation("修改")
    @PostMapping("updateStationInfo")
    public SysResult updateStationInfo(@RequestBody StationInfo stationInfo) {
        Integer rows = this.nyfwzdkService.updateStationInfo(stationInfo);
        return SysResult.success(new ArrayList<>(), rows);
    }

    @ApiOperation("删除")
    @GetMapping("deleteStationInfo")
    public SysResult deleteStationInfo(String stationid) {
        Integer deleteStationInfo = this.nyfwzdkService.deleteStationInfo(stationid);
        if (deleteStationInfo > 0) {
            List<StationInfo> allOrder = this.nyfwzdkService.getAllOrder();
            return SysResult.success(allOrder);
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("上传图片")
    @PostMapping("/uploadImage")
    public SysResult saveQhImage(MultipartFile staImage, String stationid) throws IOException {
        HyImageFile hyImageFile = nyfwzdkService.saveStaImage(staImage, stationid);
        return SysResult.success(hyImageFile);
    }

    /*
     * 实现实现时间：2024.04.22
     * 实现人员：夏凌
     * 实现功能：批量导入站点信息
     * 测试时间：2024.04.22
     * todo 测试结果有问题，部分联系人电话添加到数据表应该为null却是空格 待解决
     * */

    @ApiOperation("批量导入站点")
    @PostMapping("/importStationInfo")
    @ResponseBody
    public SysResult importStationInfo(MultipartFile file) {
        if (file.isEmpty()) {
            return returnInformation("导入站点信息", null);
        }

        List<StationInfo> stationInfoList = new ArrayList<>();
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();

            Workbook workbook = null;
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            if (".xls".equals(fileType)) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (".xlsx".equals(fileType)) {
                workbook = new XSSFWorkbook(inputStream);
            } else {
                return returnInformation("导入站点信息", null);
            }
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                for (Row row : sheet) {
                    if (row.getRowNum() > 0) { // 假设第一行为标题行
                        StationInfo stationInfo = new StationInfo();
                        for (Cell cell : row) {
                            int columnIndex = cell.getColumnIndex();
                            switch (columnIndex) {
                                case 0: stationInfo.setStationid(cell.getStringCellValue()); break;
                                case 1: stationInfo.setStationname(cell.getStringCellValue()); break;
                                case 2: stationInfo.setStationtype(cell.getStringCellValue()); break;
                                case 3: stationInfo.setCounty(cell.getStringCellValue());; break;
                                case 4: stationInfo.setTownship(cell.getStringCellValue()); break;
                                case 5: stationInfo.setVillage(cell.getStringCellValue()); break;
                                case 6: stationInfo.setCity(cell.getStringCellValue()); break;
                                case 7: stationInfo.setLon(cell.getNumericCellValue()); break;
                                case 8: stationInfo.setLat(cell.getNumericCellValue()); break;
                                case 9: stationInfo.setAlti(cell.getNumericCellValue()); break;
                                case 10: stationInfo.setForecaststationjoin(cell.getStringCellValue()); break;
                                case 11: stationInfo.setForecaststationid(String.valueOf((int)cell.getNumericCellValue())); break;
                                case 12: stationInfo.setSmallscalestationjoin(cell.getStringCellValue()); break;
                                case 13: stationInfo.setSmallscalestationid(String.valueOf((int)cell.getNumericCellValue())); break;
                                case 14: stationInfo.setSoilstationid(String.valueOf((int)cell.getNumericCellValue())); break;
                                case 15: stationInfo.setBasicsituation(cell.getStringCellValue()); break;
                                case 16: stationInfo.setPic(cell.getStringCellValue()); break;
                                case 17: stationInfo.setMaincrop(cell.getStringCellValue()); break;
                                case 18: stationInfo.setMaincroprepweaserproducts(cell.getStringCellValue()); break;
                                case 19: stationInfo.setMaincropanni(String.valueOf(cell.getNumericCellValue())); break;
                                case 20: stationInfo.setFarmmonitorelement(cell.getStringCellValue()); break;
                                case 21: stationInfo.setFarminfopubway(cell.getStringCellValue()); break;
                                case 22: stationInfo.setInfoofficer(cell.getStringCellValue()); break;
                                case 23:
                                    if (cell.getNumericCellValue() == 0){
                                        stationInfo.setContact1("");
                                    }else {
                                        stationInfo.setContact1(String.valueOf((long)cell.getNumericCellValue()));
                                    }
                                    break;
                                case 24:
                                    if (cell.getNumericCellValue() == 0){
                                        stationInfo.setContact2("");
                                    }else {
                                        stationInfo.setContact2(String.valueOf((long)cell.getNumericCellValue()));
                                    }
                                    break;
                                case 25:
                                    if (cell.getNumericCellValue() == 0){
                                        stationInfo.setContact3("");
                                    }else {
                                        stationInfo.setContact3(String.valueOf((long)cell.getNumericCellValue()));
                                    }
                                    break;
                                case 26: stationInfo.setType((short) cell.getNumericCellValue()); break;
                                case 27: stationInfo.setStatus((short) cell.getNumericCellValue()); break;

                                default: break;
                            }

                        }
                        stationInfoList.add(stationInfo);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        Integer importStationInfo = this.nyfwzdkService.importStationInfo(stationInfoList);
        return returnInformation("导入站点信息", importStationInfo);
    }

    public SysResult returnInformation(String info, Integer result) {
        if (result < 0) {
            return SysResult.fail(info + "失败");
        } else {
            return SysResult.success(result);
        }
    }
}
